package com.maiyuren.dict_page_view_plugin;
import com.maiyuren.dict_page_view_plugin.dictpageview.*;

import androidx.annotation.NonNull;

import org.json.JSONObject;
import org.json.JSONStringer;

import java.util.List;
import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/** DictPageViewPlugin */
public class DictPageViewPlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  public static MethodChannel channel;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "dict_page_view_plugin");
    channel.setMethodCallHandler(this);

    flutterPluginBinding.getPlatformViewRegistry().registerViewFactory("com.maiyuren.sudict/dictpageview", new DictPagePlatformViewFactory());
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    }
    else if (call.method.equals("loadTabs")) {
      try {
        Map<String,Object> args = (Map<String,Object>)utils.string2map((String)call.arguments, false);
        int id = (int) args.get("id");
        DictPagePlatformView platformView = DictPagePlatformView.s_views.get(id);
        if (platformView != null) {
          platformView.view.load((List<Map<String, Object>>) args.get("info"));
        }
        result.success(true);
      } catch (Exception ex){}
    }
    else if (call.method.equals("changeTab")) {
      try {
        JSONObject args = new JSONObject((String)call.arguments);
        int id = (int) args.get("id");
        DictPagePlatformView platformView = DictPagePlatformView.s_views.get(id);
        if (platformView != null) {
          platformView.view.changeTab((int) args.get("tabIndex"));
        }
        result.success(true);
      } catch (Exception ex){}
    }
    else if (call.method.equals("loadUrl")) {
      try {
        JSONObject args = new JSONObject((String)call.arguments);
        int id = (int) args.get("id");
        DictPagePlatformView platformView = DictPagePlatformView.s_views.get(id);
        if (platformView != null) {
          platformView.view.loadUrl((int) args.get("tabIndex"), (String) args.get("url"));
        }
        result.success(true);
      } catch (Exception ex){}
    }
    else if (call.method.equals("takePhoto")) {
      try {
        JSONObject args = new JSONObject((String)call.arguments);
        int id = (int) args.get("id");
        DictPagePlatformView platformView = DictPagePlatformView.s_views.get(id);
        if (platformView != null) {
          byte[] imgData = platformView.view.takePhoto((int) args.get("tabIndex"));
          result.success(imgData);
          return;
        }
        result.success(null);
      } catch (Exception ex){}
    }
    else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
